package com.interest.web.controller.outer;


import com.alibaba.fastjson2.JSONObject;
import com.interest.business.base.RequestHeaderBean;
import com.interest.business.outer.request.OrderCreateBean;
import com.interest.business.outer.request.OrderRefundBean;
import com.interest.business.service.IInterestOrderService;
import com.interest.business.utils.OutParameterValidator;
import com.interest.common.core.controller.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 对外订单接口
 */
@RestController
@RequestMapping("/outer-api/orderv2")
public class OrderController2 extends BaseController {

    @Autowired
    private IInterestOrderService interestOrderService;

    @Autowired
    OutParameterValidator outParameterValidator;

    private static final Logger LOG = LoggerFactory.getLogger(OrderController2.class);

    /**
     * 创建订单
     * @param orderCreateBean
     * @return
     */
    @PostMapping("/create")
    public Map<String,String> create(@RequestBody OrderCreateBean orderCreateBean, HttpServletRequest request){
        Map<String,String> result = new HashMap<>(2);
        result.put("code","0");
        result.put("message","操作成功");
        try{
            RequestHeaderBean header = getHeader(request);
            LOG.info("订单创建请求数据:【header】："+ JSONObject.toJSONString(header)+"，【body】:"+JSONObject.toJSONString(orderCreateBean));
            boolean success = outParameterValidator.validate(orderCreateBean,header);
           if(!success){
               result.put("code","500");
               result.put("message","签名错误");
           }else{
                interestOrderService.createOrderV2(orderCreateBean);
           }
        }catch (Exception e){
            LOG.error("创建订单失败:",e);
            result.put("code","500");
            result.put("message","操作失败");
        }
        return result;
    }

    @PostMapping("/refund")
    public Map<String,String> refund(@RequestBody  OrderRefundBean orderRefundBean, HttpServletRequest request){
        Map<String,String> result = new HashMap<>(2);
        result.put("code","0");
        result.put("message","操作成功");
        try{
            RequestHeaderBean header = getHeader(request);
            LOG.info("订单退款请求数据:【header】："+ JSONObject.toJSONString(header)+"，【body】:"+JSONObject.toJSONString(orderRefundBean));
            boolean success = outParameterValidator.validate(orderRefundBean,header);
            if(!success){
                LOG.error("签名错误!");
                result.put("code","500");
                result.put("message","签名错误");
            }else{
                interestOrderService.refundOrderV2(orderRefundBean);
            }
        }catch (Exception e){
            LOG.error("创建订单失败:",e);
            result.put("code","500");
            result.put("message","操作失败");
        }
        return result;
    }


    private RequestHeaderBean getHeader(HttpServletRequest request){
        RequestHeaderBean requestHeaderBean = new RequestHeaderBean();
        requestHeaderBean.setRequestNo(request.getHeader("requestNo"));
        requestHeaderBean.setTimestamp(request.getHeader("timestamp"));
        requestHeaderBean.setNonceStr(request.getHeader("nonceStr"));
        requestHeaderBean.setSignature(request.getHeader("signature"));
        return requestHeaderBean;
    }
}
